package com.office.supplies.controller;

import com.github.pagehelper.PageInfo;
import com.office.supplies.pojo.Employee;
import com.office.supplies.pojo.JwtProperties;
import com.office.supplies.pojo.Result;
import com.office.supplies.pojo.vo.EmployeeLoginVO;
import com.office.supplies.service.EmployeeService;
import com.office.supplies.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author admin
 * @Description 员工接口
 * @Date 2023/12/09/19:56
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 员工注册
     *
     * @param employee Employee
     * @return Result<String>
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody Employee employee) {
        log.info("员工注册,{}", employee);
        try {
            employeeService.registerEmployee(employee);
        } catch (Exception ex) {
            String message = ex.getMessage();
            if (message.contains("Duplicate entry")) {
                String[] split = message.split(" ");
                String username = split[9];
                String msg = username + "已存在";
                log.info(message);
                return Result.error(msg);
            } else {
                return Result.error("未知错误");
            }
        }
        return Result.success("注册成功!");
    }

    /**
     * 员工登录
     *
     * @param employee Employee
     * @return Result<String>
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody Employee employee) {
        log.info("员工登录：{}", employee);
        Employee employeeInfo = employeeService.login(employee);

        //生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", employeeInfo.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        return Result.success(EmployeeLoginVO.builder()
                .token(token)
                .id(employeeInfo.getId())
                .username(employeeInfo.getName())
                .userType(employeeInfo.getUserType())
                .build());
    }

    /**
     * 根据id获取员工信息
     *
     * @param id Long
     * @return Result<Employee>
     */
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable Long id) {
        log.info("根据id获取员工信息：{}", id);
        Employee employee = employeeService.getEmployeeById(id);
        return Result.success(employee);
    }

    /**
     * 获取员工列表
     *
     * @param pageNum  Integer
     * @param pageSize Integer
     * @param employee Employee
     * @return Result<Page < Employee>>
     */
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo<Employee>> getEmployeeList(@PathVariable("pageNum") Integer pageNum,
                                                      @PathVariable("pageSize") Integer pageSize,
                                                      Employee employee) {
        log.info("获取员工列表,{},{},{}", pageNum, pageNum, employee);
        return Result.success(employeeService.getAllEmployees(pageNum, pageSize, employee));
    }

    /**
     * 修改员工信息
     *
     * @param employee Employee
     */
    @PutMapping
    public Result<String> updateEmployee(@RequestBody Employee employee) {
        log.info("修改员工信息,{}", employee);
        employeeService.updateEmployee(employee);
        return Result.success("修改成功!");
    }


    /**
     * 删除员工信息
     *
     * @param id Integer
     */
    @DeleteMapping("/{id}")
    public Result<String> updateEmployee(@PathVariable Long id) {
        log.info("删除员工信息,{}", id);
        employeeService.deleteEmpLoyee(id);
        return Result.success("删除成功!");
    }

}
